#!/bin/bash
# SYNOPSIS
#	check_pbsmom [<TCP port>] [<TCP port>] ...
#
# DESCRIPTION
#	This NAGIOS plugin checks whether: 1) pbs_mom is running and
#	2) the host is listening on the given port(s).  If no port
#	number is specified TCP ports 15002 and 15003 are checked.
#
# AUTHOR
#	Wayne.Mallett@jcu.edu.au

OK=0
WARN=1
CRITICAL=2
PATH="/bin:/sbin:/usr/bin:/usr/sbin"
previous=''
# Default listening ports are TCP 15004 and 42559.
if [ $# -lt 1 ] ; then
  list="15002 15003"
else
  list="$*"
fi

if [ `ps -C pbs_mom | wc -l` -lt 2 ]; then
  filename=`date +'%Y%m%d'`
  echo "PBS_MOM CRITICAL:  Daemon is NOT running!" >> /home/cc/kanbw/multi-Daemon-log/$filename
  echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/cc/kanbw/multi-Daemon-log/$filename
  echo "PBS_MOM CRITICAL:  Daemon is NOT running!"
  checkconfigure=`chkconfig --list pbs_mom | grep ":on"`
  if [ "$checkconfigure" ];then
	/etc/init.d/pbs_mom restart
	exit $CRITICAL
  fi
else
  for port in $list ; do
    if [ `netstat -ln | grep -E "tcp.*:$port" | wc -l` -lt 1 ]; then
	  filename=`date +'%Y%m%d'`
      echo "PBS_MOM CRITICAL:  Host is NOT listening on TCP port $port!" >> /home/cc/kanbw/multi-Daemon-log/$filename
	  echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/cc/kanbw/multi-Daemon-log/$filename
	  /etc/init.d/pbs_mom restart
      echo "PBS_MOM CRITICAL:  Host is NOT listening on TCP port $port!"
      exit $CRITICAL
    fi
  done
  
#  exit $OK
fi

if [ `ps aux| grep '/usr/sbin/pbs_mom -q -d /var/spool/pbs' | grep -v grep | wc -l` -eq 1 ];then
	filename=`date +'%Y%m%d'`
	echo "PBS_MOM Warnning : pbs_mom Daemon has the parameter with -q" >> /home/cc/kanbw/multi-Daemon-log/$filename
	#ps aux|grep '/usr/sbin/pbs_mom -q -d /var/spool/pbs' | grep -v grep | awk -F' ' '{system("kill -9 " $2)}'
	/etc/init.d/pbs_mom restart >> /home/cc/kanbw/multi-Daemon-log/$filename
	echo "PBS_MOM Warnning : pbs_mom Daemon has the parameter with -q"
	exit $WARN
fi

if [ `ps aux| grep /usr/sbin/pbs_mom | grep -v grep |wc -l` != 1 ]; then
	
	checkconfigure=`chkconfig --list pbs_mom | grep ":on"`
  	if [ "$checkconfigure" ];then
  		filename=`date +'%Y%m%d'`
	        echo "PBS_MOM Warnning : multi-Daemon started" >> /home/cc/kanbw/multi-Daemon-log/$filename
        	echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/cc/kanbw/multi-Daemon-log/$filename
        	ps aux|grep /usr/sbin/pbs_mom |grep -v grep| awk -F' ' '{system("/bin/kill -9 " $2)}'
        	/etc/init.d/pbs_mom restart
        	echo "PBS_MOM Warnning : multi-Daemon started"
        	exit $WARN

	fi
	
fi

if [ `ps aux| grep /usr/sbin/pbs_mom | grep -v grep |grep root |wc -l` -ne 1 ];then

	checkconfigure=`chkconfig --list pbs_mom | grep ":on"`
        if [ "$checkconfigure" ];then
		echo "PBS_MOM CRITICAL : Daemon is NOT running as root" >> /home/cc/kanbw/multi-Daemon-log/$filename
		echo HOSTNAME: `hostname`, TIME: `date +'%H:%M:%S'` >> /home/cc/kanbw/multi-Daemon-log/$filename
		ps aux|grep /usr/sbin/pbs_mom |grep -v grep| awk -F' ' '{system("kill -9 " $2)}'
		/etc/init.d/pbs_mom restart
		exit $CRITICAL
	#else 
	#	chkconfig --level 345 pbs_mom on 
	#	/etc/init.d/pbs_mom restart
	fi
	
fi
	echo "PBS_MOM OK:  Daemon is running.  Host is listening."
	exit $OK
